#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"
#define Dispaly_Angle		1
#define Dispaly_Journey		2
/* widgets:
Layer_BaiYeLian
Background_BaiYeLian
Container_BYL_Angle_On
Container_BYLAngle_Button
PopupButton_BYLAngleEn
PopupButton_BYLJourneyDis
Container_BYLAngle_Text
Text_BYLAngle_Du
Text_BYLAngle_JiaoDu
Meter_BYLAngle_Xuanzhuan
Icon_BYLAngle_Pointer
CircleProgressBar_BYLAngle
Text_BYL_AngleNum
Icon_BYLAngle_Jingdu
Container_BYL_Journey_On
Container_BYLJourney_Button
PopupButton_BYLAngleDis
PopupButton_BYLJourneyEn
Container_BYLJourney_Text
Text_BYLJourney_BaiFengHao
Text_BYLJourney_XingCheng
Meter_BYLJourney_Xuanzhuan
Icon_BYLJourney_Pointer
CircleProgressBar_BYLJourney
Text_BYL_JourneyPercentNum
Icon_BYLJourney_Jingdu
Button_Back_MainMenu
*/
static ITUBackground* Background_BaiYeLian;
static ITUContainer* Container_BYL_Journey_On;
static ITUContainer* Container_BYL_Angle_On;
static ITUMeter* Meter_BYLJourney_Xuanzhuan;
static ITUMeter* Meter_BYLAngle_Xuanzhuan;
static ITUText* Text_BYLAngle_Du, *Text_BYLJourney_BaiFengHao;
static ITUText* Text_BYLAngle_JiaoDu, *Text_BYLJourney_XingCheng;
static uint8_t BaiYe_Angle_Or_Journey_Select = Dispaly_Journey;
bool BaiYeLianOnEnter(ITUWidget* widget, char* param)
{
	if (!Background_BaiYeLian)//首次加载
	{
		Background_BaiYeLian = ituSceneFindWidget(&theScene, "Background_BaiYeLian");
		assert(Background_BaiYeLian);
		Container_BYL_Journey_On = ituSceneFindWidget(&theScene, "Container_BYL_Journey_On");
		assert(Container_BYL_Journey_On);
		Container_BYL_Angle_On = ituSceneFindWidget(&theScene, "Container_BYL_Angle_On");
		assert(Container_BYL_Angle_On);
		Meter_BYLJourney_Xuanzhuan = ituSceneFindWidget(&theScene, "Meter_BYLJourney_Xuanzhuan");
		assert(Meter_BYLJourney_Xuanzhuan);
		Meter_BYLAngle_Xuanzhuan = ituSceneFindWidget(&theScene, "Meter_BYLAngle_Xuanzhuan");
		assert(Meter_BYLAngle_Xuanzhuan);
		Text_BYLAngle_Du = ituSceneFindWidget(&theScene, "Text_BYLAngle_Du");
		assert(Text_BYLAngle_Du);
		Text_BYLJourney_BaiFengHao = ituSceneFindWidget(&theScene, "Text_BYLJourney_BaiFengHao");
		assert(Text_BYLJourney_BaiFengHao);
		Text_BYLAngle_JiaoDu = ituSceneFindWidget(&theScene, "Text_BYLAngle_JiaoDu");
		assert(Text_BYLAngle_JiaoDu);
		Text_BYLJourney_XingCheng = ituSceneFindWidget(&theScene, "Text_BYLJourney_XingCheng");
		assert(Text_BYLJourney_XingCheng);
	}
	ituMeterSetValue(Meter_BYLJourney_Xuanzhuan, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Motor_Status.route);
	ituMeterSetValue(Meter_BYLAngle_Xuanzhuan, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Motor_Status.angle);

	//对标题进行语言转换
	if (Sys.Language == MG_LANGUAGE_CHS || Sys.Language == MG_LANGUAGE_ENG)
	{
		Text_BYLAngle_JiaoDu->fontIndex = 0;
		Text_BYLAngle_JiaoDu->boldSize = 1;
		ituTextSetFontSize(Text_BYLAngle_JiaoDu, 18);
		Text_BYLJourney_XingCheng->fontIndex = 0;
		Text_BYLJourney_XingCheng->boldSize = 1;
		ituTextSetFontSize(Text_BYLJourney_XingCheng, 18);
	}
	else if (Sys.Language == MG_LANGUAGE_ZHTW)
	{
		Text_BYLAngle_JiaoDu->fontIndex = 8;
		Text_BYLAngle_JiaoDu->boldSize = 0;
		ituTextSetFontSize(Text_BYLAngle_JiaoDu, 16);
		Text_BYLJourney_XingCheng->fontIndex = 8;
		Text_BYLJourney_XingCheng->boldSize = 0;
		ituTextSetFontSize(Text_BYLJourney_XingCheng, 16);
	}
	//对“°”坐标判断
	if (Meter_BYLAngle_Xuanzhuan->value >= 100)
		ituWidgetSetX(Text_BYLAngle_Du, 141);
	else
		ituWidgetSetX(Text_BYLAngle_Du, 123);
	//对“%”坐标判断
	if (Meter_BYLJourney_Xuanzhuan->value >= 100)
		ituWidgetSetX(Text_BYLJourney_BaiFengHao, 141);
	else
		ituWidgetSetX(Text_BYLJourney_BaiFengHao, 123);

	Set_Rotate_Block(30);//旋转步进
    return true;
}

bool BaiYeLianOnLeave(ITUWidget* widget, char* param)
{
    return true;
}

bool BaiYeLianOnTimer(ITUWidget* widget, char* param)
{
	static int MeterJourneyValue = 0, MeterJourneyValue_L = 0;
	static int MeterAngleValue = 0, MeterAngleValue_L = 0;
	static uint32_t SendEndTime_J = Rotate_SendCmd_Time, SendTime_J = 0; 
	static uint32_t SendEndTime_A = Rotate_SendCmd_Time, SendTime_A = 0;
	//用户手势操作
	if (Action_Cmd.User_Action == Left_Slide || Action_Cmd.User_Action == Right_Slide)
	{
		GotoMainMenu();//返回主菜单
		Clear_User_Action();
	}

	//设备操作
	if (BaiYe_Angle_Or_Journey_Select == Dispaly_Journey)//行程设置界面
	{
		MeterJourneyValue = Meter_BYLJourney_Xuanzhuan->value;
		if (Action_Cmd.User_Action == Clockwise_Rotation && MeterJourneyValue <100)//有用户操作
		{
			MeterJourneyValue += Action_Cmd.Action_Times;
			if (MeterJourneyValue > 100)MeterJourneyValue = 100;
			ituMeterSetValue(Meter_BYLJourney_Xuanzhuan, MeterJourneyValue);//设置Meter百分比值
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
			Clear_User_Action();
		}
		else if (Action_Cmd.User_Action == Anticlockwise_Rotation && MeterJourneyValue > 0)
		{
			MeterJourneyValue -= Action_Cmd.Action_Times;
			if (MeterJourneyValue < 0)MeterJourneyValue = 0;
			ituMeterSetValue(Meter_BYLJourney_Xuanzhuan, MeterJourneyValue);//设置Meter百分比值
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
			Clear_User_Action();
		}

		//发送执行报文
		if (MeterJourneyValue == MeterJourneyValue_L)
		{
			if (SendEndTime_J <= Rotate_SendCmd_Time)
				SendEndTime_J++;
			//停止操作0.6s后发送执行报文
			if (SendEndTime_J == Rotate_SendCmd_Time)
			{
				SendTime_J = 0;
				RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
				MessageFlag[MSG_GET_DEVICE_STATE_EN] = ON;//允许10s自动获取状态
			}
		}
		else
		{
			SendEndTime_J = 0;
			TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Motor_Status.route = MeterJourneyValue;
			TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Motor_Status.status = 0x14;//命令设置行程
			TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Motor_Status.route = MeterJourneyValue;
		}
		//用MSG_GET_DEVICE_STATE_EN标记来表示当前是否在旋转动作中
		if (MessageFlag[MSG_GET_DEVICE_STATE_EN] == OFF)
		{
			if (SendTime_J < Rotating_SendCmd_Time)
				SendTime_J++;
			if (SendTime_J == Rotating_SendCmd_Time)//0.495s发送一次命令
			{
				SendTime_J = 0;
				RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
			}
		}
		//对“%”坐标判断
		if (Meter_BYLJourney_Xuanzhuan->value >= 100)
			ituWidgetSetX(Text_BYLJourney_BaiFengHao, 141);
		else
			ituWidgetSetX(Text_BYLJourney_BaiFengHao, 123);
	}
	else if (BaiYe_Angle_Or_Journey_Select == Dispaly_Angle)//角度设置界面
	{
		MeterAngleValue = Meter_BYLAngle_Xuanzhuan->value;
		if (Action_Cmd.User_Action == Clockwise_Rotation && MeterAngleValue < 180)//有用户操作
		{
			MeterAngleValue += Action_Cmd.Action_Times;
			if (MeterAngleValue > 180)MeterAngleValue = 180;
			ituMeterSetValue(Meter_BYLAngle_Xuanzhuan, MeterAngleValue);//设置Meter百分比值
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
			Clear_User_Action();
		}
		else if (Action_Cmd.User_Action == Anticlockwise_Rotation && MeterAngleValue > 0)
		{
			MeterAngleValue -= Action_Cmd.Action_Times;
			if (MeterAngleValue < 0)MeterAngleValue = 0;
			ituMeterSetValue(Meter_BYLAngle_Xuanzhuan, MeterAngleValue);//设置Meter百分比值
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
			Clear_User_Action();
		}

		//发送执行报文
		if (MeterAngleValue == MeterAngleValue_L)
		{
			if (SendEndTime_A <= Rotate_SendCmd_Time)
				SendEndTime_A++;
			//停止操作0.6s后发送执行报文
			if (SendEndTime_A == Rotate_SendCmd_Time)
			{
				SendTime_A = 0;
				RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
				MessageFlag[MSG_GET_DEVICE_STATE_EN] = ON;//允许10s自动获取状态
			}
		}
		else
		{
			SendEndTime_A = 0;
			TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Motor_Status.angle = MeterAngleValue;
			TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Motor_Status.status = 0x15;//命令设置角度
			TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Motor_Status.angle = MeterAngleValue;
		}
		//用MSG_GET_DEVICE_STATE_EN标记来表示当前是否在旋转动作中
		if (MessageFlag[MSG_GET_DEVICE_STATE_EN] == OFF)
		{
			if (SendTime_A < Rotating_SendCmd_Time)
				SendTime_A++;
			if (SendTime_A == Rotating_SendCmd_Time)//0.495s发送一次命令
			{
				SendTime_A = 0;
				RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
			}
		}
		MeterAngleValue_L = MeterAngleValue;
		//对“°”坐标判断
		if (Meter_BYLAngle_Xuanzhuan->value >= 100)
			ituWidgetSetX(Text_BYLAngle_Du, 141);
		else
			ituWidgetSetX(Text_BYLAngle_Du, 123);
	}
	//显示状态更新
	if (MessageFlag[MSG_GUI_STATE_UPDATE] == ON)
	{
		ituMeterSetValue(Meter_BYLJourney_Xuanzhuan, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Motor_Status.route);
		ituMeterSetValue(Meter_BYLAngle_Xuanzhuan, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Motor_Status.angle);
		MeterJourneyValue = Meter_BYLJourney_Xuanzhuan->value;//防止循环执行
		MeterAngleValue = Meter_BYLAngle_Xuanzhuan->value;//防止循环执行
		MessageFlag[MSG_GUI_STATE_UPDATE] = OFF;
	}
	MeterJourneyValue_L = MeterJourneyValue;
	MeterAngleValue_L = MeterAngleValue;
    return true;
}
bool PopupButton_BYLJourneySet(ITUWidget* widget, char* param)
{
	BaiYe_Angle_Or_Journey_Select = Dispaly_Journey;
	return true;
}

bool PopupButton_BYLAngleSet(ITUWidget* widget, char* param)
{
	BaiYe_Angle_Or_Journey_Select = Dispaly_Angle;
	return true;
}

